﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace SchoolPointe.Menus.Enums
{
    public static class EnumExtensions
    {
        public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
        {
            var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                         select new { ID = e, Name = e.Description() };

            return new SelectList(values, "Id", "Name", enumObj);
        }

        public static string Description<TEnum>(this TEnum enumObject)
        {
            Type type = enumObject.GetType();
            MemberInfo[] memInfo = type.GetMember(enumObject.ToString());

            if (memInfo != null && memInfo.Length > 0)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes.Length > 0)
                {
                    return attributes[0].Description;
                }
            }

            return enumObject.ToString();

        }
    }
}